package spider;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import jxl.write.WriteException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class xwqyjdzb {
	
	private static HttpClient httpClient = null;
	private static Long lastTime = null;
	private static String folderName = "xwqyjdzb";
	
	/*领导和这个数据库干上了···请你继续通过网络爬虫的方式，下载以下数据。
    首先，仍然进入老地方http://www.csmbri.com/front/index.htm； 
    在右侧登入账号：15120046710   密码：xiaoping0321
    进入之后，从左侧点击“景气指数”，需要你爬“小微企业经济指标”，共获取 381,313 记录。*/
	
	//381313
	public static void main(String[] args) {
			doIt();
	}
	
	public static void doIt(){
		getFile(1);
		getFile(101);
		getFile(201);
		getFile(301);
		getFile(401);
		getFile(501);
		getFile(601);
		getFile(701);
		getFile(801);
		getFile(901);
		getFile(1001);
		getFile(1101);
		getFile(1201);
		
	}
	
	public static void getFile(int pageIndex) {
		
		String fileName =pageIndex+".xlsx";
		Long s = null;
		Long e = null;
		int maxPage = pageIndex + 100;
			for(int page=pageIndex;page<maxPage;page++){
				try {
					s = new Date().getTime();
					System.out.println("正在拉取第："+page+"页数据，还剩："+(maxPage-page-1)+"页数据");
					JSONObject pageData = getData(page);
//					writeData(pageData,"c://"+folderName,fileName);
					writeData(pageData,File.separator+"root"+File.separator+folderName,fileName);
					e = new Date().getTime();
					System.out.println("写入第一页成功,耗时："+(e-s)/1000+"秒");
				} catch (Exception ee) {
					ee.printStackTrace();
					System.out.println("写入第页"+page+"失败："+ee.getMessage());
				}
			}
	}
	
public static void writeData(JSONObject ret,String floder,String fileName) throws WriteException, FileNotFoundException, Exception{
		
		File ff= new File(floder);
		if(!ff.exists()){
			ff.mkdir();
		}
		String[] title = new String[]{
				"序号","企业名称","日期","工业总产值","出口交货值","营业收入","营业成本","期末借款余额","财务费用","应收账款",
				"产成品","利润总额","应缴税款","从业人员","用电量","生产能力(设备)利用率","应付职工薪酬","资产总计","负债总计","固定资产投资额",
				};
		
		
		if("0".equals(ret.getString("code")))
		{
			JSONArray rows = ret.getJSONArray("data");
			List<Object[]> list = new ArrayList<Object[]>();
			for(int i=0;i<rows.size();i++)
			{
				JSONObject row = rows.getJSONObject(i);
				Object[] data = new Object[title.length];
				data[0] = row.getString("id");
				data[1] = trans(row.getString("f002"));//*企业名称
				data[2] = row.getString("bdate");//日期
				data[3] = row.getString("f003");
				data[4] = row.getString("f007");//市
				data[5] = row.getString("f011");//*
				data[6] = row.getString("f015");//*
				data[7] = row.getString("f019");//*
				data[8] = row.getString("f023");//*
				data[9] = row.getString("f027");//*填表负责人
				
				data[10] = row.getString("f031");//*
				data[11] = trans(row.getString("f035"));//*联系电话
				data[12] = trans(row.getString("f039"));//*手机
				data[13] = row.getString("f043");//*
				data[14] = row.getString("f047");//1
				data[15] = row.getString("f051");
				data[16] = row.getString("f055");
				data[17] = row.getString("f059");
				data[18] = row.getString("f063");
				data[19] = row.getString("f067");
				
				list.add(data);
			}
			File f= new File(floder+File.separator+fileName);
			//写入Excel
			ExcelUtil.createExcel(f, 
					list, title, "工业产销监测数据", new String[]{
							"30","30","30","30","30","30","30","30","30","30",
							"30","30","30","30","30","30","30","30","30","30"
							},f.exists());
		}else
		{
			System.out.println("拉取数据异常");
		}
		
		
		
	}
	
	public static String trans(String str){
		try {
			BigDecimal db = new BigDecimal(str);
			return db.toString();
		} catch (Exception e) {
			return str;
		}
	}
	
    public static HttpClient login() throws Exception {
    	// 先判断是否过期
    	if(null != httpClient){
    		if((new Date().getTime()-lastTime)/1000 < 10*60*1000L){
    			return httpClient;
    		}
    	}
    	
    	httpClient = new DefaultHttpClient();
        HttpResponse response = null;
        HttpEntity entity = null;
 
        HttpPost httppost = new HttpPost("http://www.csmbri.com/login");
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username", "15120046710"));
        params.add(new BasicNameValuePair("password", "xiaoping0321"));
        httppost.setEntity(new UrlEncodedFormEntity(params));
 
        response = httpClient.execute(httppost);
        entity = response.getEntity();
        // 在这里可以用Jsoup之类的工具对返回结果进行分析，以判断登录是否成功
        String postResult = EntityUtils.toString(entity, "GBK");
        System.out.println(postResult);
        if(null == postResult || postResult.equals("")){
        	//登陆成功
        	System.out.println("登陆成功");
        	lastTime = new Date().getTime();
        }else{
        	//登陆失败
        	httpClient = null;
        	lastTime = null;
        }
        return httpClient;
    }
   
    
    public static JSONObject getData(int pageNo) throws Exception{
    	if(null == httpClient){
    		httpClient = login();
    		if(null == httpClient){
    			System.out.println("登陆失败");
    		}
    	}
        String memberpage = "http://www.csmbri.com/admin2/ifo/ei/list";
        HttpPost httppost = new HttpPost(memberpage);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        int start = (pageNo-1)*300;
        params.add(new BasicNameValuePair("start", start+""));
        params.add(new BasicNameValuePair("length", "300"));
        httppost.setEntity(new UrlEncodedFormEntity(params));
        
        HttpResponse response = httpClient.execute(httppost);
        HttpEntity entity = response.getEntity();
        // 在这里可以用Jsoup之类的工具对返回结果进行分析，以判断登录是否成功
        String postResult = EntityUtils.toString(entity, "GBK");
        JSONObject ret =JSONObject.fromObject(postResult);
        return ret;
    } 
}